import { Get, Controller, Response, HttpStatus, Param, Post, Body, Put, Delete, UsePipes, HttpException, Inject, UseFilters, NotFoundException, ForbiddenException, UseGuards, UseInterceptors } from '@nestjs/common'
import { UserService } from './user.service'
import { User } from 'src/interface/user/user.interface'
import { ValidationPipe } from '../../share/pipe/validate'
import { ApiUseTags, ApiResponse, ApiBearerAuth } from '@nestjs/swagger'
import { HttpExceptionFilter } from 'src/common/exceptionFilter/http-exception.filter';
import { RolesGuard } from 'src/share/guards/role.guard'
import { TransformInterceptor } from 'src/share/interceptor/transform.interceptor';
import { LoggerInterceptor }  from 'src/share/interceptor/logger.interceptor';
import { Roles } from 'src/decorator/roles.decorator';
import { MyUser } from 'src/decorator/user.decorator';
import { AuthGuard } from '@nestjs/passport'

@UseInterceptors(TransformInterceptor, LoggerInterceptor)
@UseGuards(RolesGuard)
@UseFilters(HttpExceptionFilter)
@Controller('/user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @ApiResponse({ status: 200, description: 'you have gotten all datas!' })
  @ApiBearerAuth()
  @ApiUseTags('user')
  @Get('/findAll')
  // @UseGuards(AuthGuard('bearer'))
  async findAll() {
    const users = await this.userService.findAll()
    // res.status(HttpStatus.OK).json(users)
    return users
  }

  // @UseFilters(new HttpExceptionFilter())
  @ApiUseTags('user')
  @Get('/findOne/:id')
  async findOne(@Param('id') id, @Response() res) {
    const user = await this.userService.findOne(+id)
    if (user) {
      res.status(HttpStatus.OK).json(user)
    } else {
      // throw new ForbiddenException()
      throw new HttpException('错误', HttpStatus.FORBIDDEN)
    }
  }

  @Roles('admin')
  @ApiUseTags('user')
  @Post('/create')
  async create(@Body() user: User, @MyUser(new ValidationPipe()) body: User) {
    console.log(body, 123)
    const newUser = await this.userService.create(user)
    // // return newUser
    // res.status(HttpStatus.CREATED).json(newUser)
    return newUser
  }

  @ApiUseTags('user')
  @Put('/edit')
  async edit(@Body() user, @Response() res) {
    const newUser = await this.userService.edit(user)
    res.status(HttpStatus.OK).json(newUser)
  } 

  @ApiUseTags('user')
  @Delete('/remove/:id')
  async remove(@Param('id') id, @Response() res) {
    const bool = await this.userService.remove(+id)
    res.status(HttpStatus.OK).json(bool)
  }
}